
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/base/handle_mixin.dart';
import 'package:gczn_hd/app/core/values/App_img.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/core/widget/custom_title_bar.dart';
import 'package:gczn_hd/app/modules/ppm/create_report/controllers/create_report_controller.dart';
import 'package:get/get.dart';
import '../../../../core/values/text_styles.dart';

class FormRight extends StatelessWidget with HandleMixin{
  final CreateReportController controller = Get.put(CreateReportController());


  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        CustomTitleBar(title: "报工录入", handleBtn: Row(
          children: [
            Text("自动计数差：322", style: textStyle20Opa8,),
            SizedBox(width: AppValues.size108.w,),
            Text("本次良品统计：300", style: textStyle20Opa8,),
          ],
        ),),
        Expanded(
          flex: 1,
          child: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: AppValues.size30.h,),
                _buildCounter(title: '本次报工数量', inputController: controller.reportInputController,),
                SizedBox(height: AppValues.size30.h,),
                _buildCounter(title: '本次不良品数量', inputController: controller.badInputController,),
                SizedBox(height: AppValues.size30.h,),
                _buildBadList(context), // 不良品输入
                SizedBox(height: AppValues.size30.h,),
                _buildUpload(),
                SizedBox(height: AppValues.size30.h,),
                _buildRemark()
              ],
            ),
          ),
        )

      ],
    );
  }


  Widget _buildRemark(){
    return Container(
      padding:  EdgeInsets.symmetric(horizontal: AppValues.size20.w),
      alignment: Alignment.topLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text("备注", style: textStyle,),
          SizedBox(height: AppValues.size20.h,),
          SizedBox(
            child: TextField(
              controller: controller.remarknputController,
              maxLines: 3,
                decoration: InputDecoration(
                  border: const OutlineInputBorder(
                      borderSide: BorderSide.none,
                      borderRadius: BorderRadius.zero
                  ),
                  focusedBorder: const OutlineInputBorder(
                    borderSide: BorderSide.none,
                  ),
                  filled: true,
                  fillColor: AppColors.gcznPrimaryColor,
                  hintText: '请输入',
                  hintStyle: textStyle,
                  contentPadding: EdgeInsets.symmetric(horizontal: AppValues.size20.w, vertical: AppValues.size20.h),
                )
            ),
          )
        ],
      ),
    );
  }

  /// 上传
  Widget _buildUpload(){

    return Container(
      padding:EdgeInsets.symmetric(horizontal: AppValues.size20.w),
      alignment: Alignment.topLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text("品质图片", style: textStyle,),
          SizedBox(height: AppValues.size20.h,),
          Wrap(
            spacing: AppValues.margin_10.w, // 水平间距
            runSpacing: AppValues.margin_10.h, // 垂直间距
            children: [
              _buildImageButton(),
              ..._buildImageList(),
            ],
          )
        ],
      ),
    );

  }

  /// 上传图片列表
  List<Widget> _buildImageList(){
    List<Widget> list = [];
    list = [1,2,3,4].map((item) => Stack(
      clipBehavior: Clip.none, // 禁用裁剪
      children: [
        Container(
          width: AppValues.size100.w,
          height: AppValues.size100.w,
          decoration: BoxDecoration(
            color: AppColors.counterBtn,
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
          ),
          child: Center(
            child: Image.asset(AppImg.defaultpic, fit: BoxFit.fill,),
          ),
        ),
        const Positioned(
          top: -5,
          right: -5,
          child: Icon(Icons.remove_circle_rounded, color: AppColors.dangerTextColor, size: 20,),
        )
      ],
    )).toList();
    

    return list;
  }

/// 上传按钮
  Widget _buildImageButton(){
    const Size iconSize = Size(31, 28);

    return  Container(
      width: AppValues.size100.w,
      height: AppValues.size100.w,
      decoration: BoxDecoration(
        color: AppColors.counterBtn,
        borderRadius: BorderRadius.circular(AppValues.margin_12.r),
      ),
      child:  Center( // 使用 Center 确保图像居中
        child: Image.asset( AppImg.uploadCamera, width: iconSize.width.w, height: iconSize.height.h, color: AppColors.colorWhite, colorBlendMode: BlendMode.srcIn, ),
      ),
    );
  }


  /// 不良品列表
  Widget _buildBadList(BuildContext context){

    return Container(
      padding:  EdgeInsets.symmetric(horizontal: AppValues.size20.w),
      alignment: Alignment.topLeft,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text("不良品项", style: textStyle,),
          SizedBox(height: AppValues.size20.h,),
          Wrap(
            spacing: AppValues.margin_10.w, // 水平间距
            runSpacing: AppValues.margin_10.h, // 垂直间距
            children: [
              _buildBadItem(text: "未收紧 1.0"),
              _buildBadItem(text: "未收紧 1.0"),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                  showToast('openDrawer');
                  Scaffold.of(context).openEndDrawer();

                },
                child: Container(
                  width: AppValues.size60.w,
                  height: AppValues.size60.w,
                  padding: EdgeInsets.zero,
                  decoration: BoxDecoration(
                    color: AppColors.counterBtn,
                    borderRadius: BorderRadius.circular(AppValues.margin_12.r),
                  ),
                  child: const Center(
                    child: Text("+", style: TextStyle(color: AppColors.colorWhite),),
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  /// 不良品项
  Widget _buildBadItem({required text}){

    return  Container(

      padding: EdgeInsets.only(left: AppValues.size28.w, right: AppValues.size28.w, top: AppValues.size12.w, bottom: AppValues.size12.w),
      decoration: BoxDecoration(
        color:AppColors.badBg,
        borderRadius: BorderRadius.circular(AppValues.margin_12.r),
      ),
      child: Text(text, style: textStyle,),
    );
  }



  /// 加减框组
  Widget _buildCounter({required String title,  required TextEditingController inputController,}){

    return Container(
      padding:  EdgeInsets.symmetric(horizontal: AppValues.size20.w),
      margin: EdgeInsets.only(top: AppValues.size20.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title, style: textStyle,),
          Container(
            margin: EdgeInsets.only(top: AppValues.size12.h),
            child: Row(
              children: [
                Row(
                  children: [
                    _buildButton(text: "-10", onPressed: (){}),
                    const SizedBox(width: 1), // 按钮间距
                    _buildButton(text: "-1", post: 2, onPressed: (){}),
                  ],
                ),
                _buildInput(inputController: inputController),
                Row(
                  children: [
                    _buildButton(text: "+1", post: 2, onPressed: (){}),
                    const SizedBox(width: 1), // 按钮间距
                    _buildButton(text: "+10", post: 3, onPressed: (){}),
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildInput({required TextEditingController inputController,}){
    const double inputWidth = 200;

    return Container(
      width: inputWidth.w,
      height:AppValues.size86.h,
      padding: const EdgeInsets.all(2),
      decoration: const BoxDecoration(
        border: Border(
          top: BorderSide(color: AppColors.counterBtn, width: 1),
          bottom: BorderSide(color: AppColors.counterBtn, width: 1)
        )
      ),
      child: TextField(
        controller: inputController,
          cursorColor: AppColors.colorWhite,
          style: whiteTextStyle, // 设置输入文本颜色
          textAlign: TextAlign.center,
        decoration: InputDecoration(
          border: const OutlineInputBorder(
            borderSide: BorderSide.none,
            borderRadius: BorderRadius.zero
          ),
          focusedBorder: const OutlineInputBorder(
            borderSide: BorderSide.none,
          ),
          filled: true,
          fillColor: AppColors.gcznPrimaryColor,
          hintText: '请输入数量',
          hintStyle: textStyle,
          contentPadding: EdgeInsets.symmetric(horizontal: AppValues.size20.w, vertical: AppValues.size20.h),
        )
      ),
    );
  }

  /// 按钮
  Widget _buildButton({required String text, required Function() onPressed, int post = 1}){


    return ElevatedButton(
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
          backgroundColor: AppColors.counterBtn,
          shape: post == 1 ?  RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(AppValues.margin_12.r),
              bottomLeft: Radius.circular(AppValues.margin_12.r),
            ),
          ) : post == 2 ? const RoundedRectangleBorder(
            borderRadius: BorderRadius.only( ),
          ) : RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(AppValues.margin_12.r),
              bottomRight: Radius.circular(AppValues.margin_12.r),
            ),
          ),
          fixedSize: Size(AppValues.size86.w, AppValues.size86.h)
      ),
      child: Text( text, style: textStyle, ),
    );

  }

}